package top.soulblack.purchasesale.controller.setting;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.model.GoodsClassify;
import top.soulblack.purchasesale.service.GoodsClassifyService;

import static top.soulblack.purchasesale.common.CodeMsg.*;

/**
 * create by 小川
 */
@Controller
@RequestMapping("/classify")
public class GoodsClassifyController {

    @Autowired
    private GoodsClassifyService goodsClassifyService;


    @RequestMapping("/addClassify")
    @ResponseBody
    public Result<Boolean> addClassify(GoodsClassify goodsClassify) {
        // 判断是否存在当前颜色
        if (goodsClassifyService.existClassify(goodsClassify.getName())) {
            return Result.error(COLOR_EXIST);
        }
        goodsClassifyService.addClassify(goodsClassify);
        return Result.success(true);
    }

    @RequestMapping("/deleteClassify")
    @ResponseBody
    // 后期需要加上User权限验证，否则暴露接口有危险。
    public Result<Boolean> deleteColor(Integer classifyId) {
        if (goodsClassifyService.deleteClassify(classifyId)) {
            return Result.success(true);
        }
        return Result.error(COLOR_DELETE);
    }
    @RequestMapping("/editClassify")
    @ResponseBody
    public Result<Boolean> editClassify(GoodsClassify goodsClassify) {
        System.out.println(goodsClassify.getRemarks());
        if (goodsClassifyService.updateClassify(goodsClassify)) {
            return Result.success(true);
        }
        return Result.error(COLOR_EDIT_ERROR);
    }

    @RequestMapping("/editGroup")
    @ResponseBody
    public Result<Boolean> editGroup(String newGroup, String oldGroup) {
        if (goodsClassifyService.updateGroup(newGroup, oldGroup)) {
            return Result.success(true);
        }
        return Result.error(GROUP_EDIT_ERROR);
    }

    @RequestMapping("/deleteGroup")
    @ResponseBody
    public Result<Boolean> editGroup(String oldGroup) {
        if (goodsClassifyService.deleteGroup(oldGroup)) {
            return Result.success(true);
        }
        return Result.error(GROUP_EDIT_ERROR);
    }

}
